package com.sdry.service.impl.lz;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sdry.mapper.llm.CancellingStockDetailMapper;
import com.sdry.mapper.llm.CancellingStockMapper;
import com.sdry.mapper.lz.InventoryMapper;
import com.sdry.model.lz.InventoryDetail;
import com.sdry.model.lz.InventoryOrder;
import com.sdry.model.lz.LzQueryCriteria;
import com.sdry.model.lz.Materiel;
import com.sdry.model.lz.Warehouse;
import com.sdry.model.zc.ZcInventoryInfoEntity;
import com.sdry.model.zc.ZcSysUserEntity;
import com.sdry.service.lz.InventoryService;

/**
 * @ClassName InventoryServiceImpl
 * @Description 盘点管理
 * @Author lz
 * @Date 2019年3月26日 14:18:37
 * @Version 1.0
 */
@Service
public class InventoryServiceImpl implements InventoryService{

	@Resource
	InventoryMapper inventoryMapper;
	@Resource
	CancellingStockDetailMapper cancellingStockDetailMapper;
	@Resource
	CancellingStockMapper cancellingStockMapper;
	
	@Override
	public Long addInventoryOrder(InventoryOrder inventoryOrder) {
		return inventoryMapper.addInventoryOrder(inventoryOrder);
	}

	@Override
	public Long addInventoryOrderDetail(InventoryDetail detail) {
		Long l = 0L;
		Long id = detail.getMateriel_id();
		//根据物料查供应商
		//if(id != null && !"".equals(id)){
		//Customer customer = cancellingStockDetailMapper.getCustomerByMid(id);
		//if(customer != null){
			Map<String, Object> map = new HashMap<>();
			map.put("mid", id);
			//map.put("customerId", customer.getId());
			//根据物料id查库存
			List<ZcInventoryInfoEntity> infoEntity = cancellingStockDetailMapper.getQuantityByMid(map);
			long sum=0;
			for (ZcInventoryInfoEntity zcInventoryInfoEntity : infoEntity) {
				sum+=zcInventoryInfoEntity.getmNum();
			}
			detail.setTheoretical_quantity(sum+0L);
			//System.out.println("***************"+sum);
			l = inventoryMapper.addInventoryOrderDetail(detail);
			//}
		//}
		return l;
	}
	
	@Override
	public Long deleteInventoryOrderById(long id) {
		return inventoryMapper.deleteInventoryOrderById(id);
	}

	@Override
	public Long deleteInventoryOrderDetailById(long id) {
		return inventoryMapper.deleteInventoryOrderDetailById(id);
	}

	@Override
	public Long updateInventoryOrderById(String id, String fieldName,
			String fieldValue) {
		return inventoryMapper.updateInventoryOrderById(id, fieldName, fieldValue);
	}

	/*@Override
	public Long updateInventoryOrderDetailById(String id, String fieldName,
			String fieldValue) {
		InventoryDetail detail = inventoryMapper.queryInventoryDetailById(Long.parseLong(id));
		detail.setActual_quantity(Long.parseLong(fieldValue));
		detail.setDifference_quantity(detail.getTheoretical_quantity()-Long.parseLong(fieldValue));
		return inventoryMapper.updateInventoryDetailAndroid(detail);
	}*/

	@Override
	public List<InventoryOrder> queryInventoryOrderCriteria(
			LzQueryCriteria criteria) {
		return inventoryMapper.queryInventoryOrderCriteria(criteria);
	}

	@Override
	public int countInventoryOrderCriteria(LzQueryCriteria criteria) {
		return inventoryMapper.countInventoryOrderCriteria(criteria);
	}
	
	@Override
	public List<InventoryDetail> queryInventoryDetailCriteriaById(
			LzQueryCriteria criteria) {
		return inventoryMapper.queryInventoryDetailCriteriaById(criteria);
	}

	@Override
	public int countInventoryDetailCriteriaById(LzQueryCriteria criteria) {
		return inventoryMapper.countInventoryDetailCriteriaById(criteria);
	}

	@Override
	public List<InventoryOrder> queryInventoryOrderCriteriaAndroid() {
		return inventoryMapper.queryInventoryOrderCriteriaAndroid();
	}

	@Override
	public List<InventoryDetail> queryInventoryDetailCriteriaByIdAndroid(
			String order_id) {
		return inventoryMapper.queryInventoryDetailCriteriaByIdAndroid(order_id);
	}

	@Override
	public Long updateInventoryDetailAndroid(InventoryDetail inventoryDetail) {
		return inventoryMapper.updateInventoryDetailAndroid(inventoryDetail);
	}

	@Override
	public List<Warehouse> queryAllWarehouseAndroid() {
		return inventoryMapper.queryAllWarehouseAndroid();
	}
	
	@Override
	public List<InventoryDetail> queryInventoryDetailByOrder(String orderId) {
		/*List<InventoryDetail> list = inventoryMapper.queryInventoryDetailByOrder(orderId);
		if(list.size() > 0){
			for(InventoryDetail detail : list){
				if(detail.getMateriel_id() != null && !"".equals(detail.getMateriel_id())){
					Materiel materiel = cancellingStockMapper.getMaterielByMid(detail.getMateriel_id());
					detail.setMateriel_name(materiel.getMateriel_name());
					detail.setMateriel_num(materiel.getMateriel_num());
					detail.setBrevity_num(materiel.getBrevity_num());
				}
			}
		}
		return list;*/
		long oid = queryInventorynum(orderId);
		return queryDetailByid(oid);
	}

	@Override
	public InventoryDetail queryInventoryDetailById(long id) {
		InventoryDetail detail = inventoryMapper.queryInventoryDetailById(id);
		if(detail.getMateriel_id() != null && !"".equals(detail.getMateriel_id())){
			Materiel materiel = cancellingStockMapper.getMaterielByMid(detail.getMateriel_id());
			detail.setMateriel_name(materiel.getMateriel_name());
			detail.setMateriel_num(materiel.getMateriel_num());
			detail.setBrevity_num(materiel.getBrevity_num());
		}
		return detail;
	}

	public List<InventoryOrder> queryAllInventoryOrder() {
		List<InventoryOrder> list = inventoryMapper.queryAllInventoryOrder();
		return list;
	}

	/**       
	 * @return    
	 */
	@Override
	public List<ZcSysUserEntity> queryAllUser() {
		return inventoryMapper.queryAllUser();
	}

	@Override
	public Long updateInventoryOrderDetailById(String mid, String fieldName,
			String fieldValue, String cid, String detailid) {
		InventoryDetail detail = inventoryMapper.queryInventoryDetailByIdcid(Long.parseLong(mid),Long.parseLong(cid),Long.parseLong(detailid));
		detail.setActual_quantity(Long.parseLong(fieldValue));
		detail.setDifference_quantity(Long.parseLong(fieldValue) - detail.getTheoretical_quantity());
		return inventoryMapper.updateInventoryDetailAndroid(detail);
	}

	@Override
	public Long queryInventorynum(String num) {
		// TODO Auto-generated method stub
		return inventoryMapper.queryInventorynum(num);
	}

	@Override
	public List<InventoryDetail> queryDetailByid(long id) {
		// TODO Auto-generated method stub
		return inventoryMapper.queryDetailByid(id);
	}
	
}